<template>
  <div class="card col-12" id="review-bill-tpr">
    <div class="card-body py-0">
      <nav class="float-end">
        <my-pagination
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </div>

    <table class="table table-sm table-striped table-hover">
      <thead>
        <tr>
          <th>出票日期</th>
          <th>票号</th>
          <th>编码/行程</th>
          <th class="text-end">价格</th>
          <th class="text-end">航段奖励</th>
          <th class="text-end">底价（不含税)</th>
          <th class="text-end">税</th>
          <th class="text-end">底价（含税）</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-if="ticketStatus === 0">
          <td colspan="3">合计</td>
          <td class="text-end">{{ totalInfo.normalFareAmount }}</td>
          <td class="text-end">{{ totalInfo.normalCommitAmount }}</td>
          <td class="text-end">{{ totalInfo.carriersAmount }}</td>
          <td class="text-end">{{ totalInfo.normalTaxAmount }}</td>
          <td class="text-end fw-bold text-danger h4">
            {{ totalInfo.normalTotalCost }}
          </td>
          <td class="">已匹配 {{ totalInfo.normalMatchedCount }} 个</td>
        </tr>
        <tr v-if="ticketStatus === 4">
          <td colspan="3">小计</td>
          <td class="text-end fw-bold text-danger h4">
            {{ totalInfo.netRefundAmount }}
          </td>
          <td class="text-end">{{ totalInfo.refundCommit }}</td>
          <td class="text-end"></td>
          <td class="text-end">{{ totalInfo.refundTaxAmount }}</td>
          <td></td>
          <td class="">已匹配 {{ totalInfo.refundMatchedCount }} 个</td>
        </tr>
        <tr v-for="flight in dataList" :key="flight.id">
          <td>
            <small>{{ flight.sellDate }}</small>
          </td>
          <td>
            {{ flight.ticketNo }}
            <span class="text-muted" v-if="flight.ticketStatus === 1"
              >改签</span
            >
            <span class="text-muted" v-if="flight.ticketStatus === 4"
              >退票</span
            >
            <span class="text-muted" v-if="flight.ticketStatus === 16">废</span>
            {{ flight.conjTag }}
          </td>
          <td>{{ flight.pnrNo }}/{{ flight.orig }}{{ flight.dest }}</td>
          <td class="text-end">{{ flight.price }}</td>
          <td class="text-end">{{ flight.commission }}</td>
          <td class="text-end">{{ flight.bottomPrice - flight.tax }}</td>
          <td class="text-end">{{ flight.tax }}</td>
          <td class="text-end">{{ flight.bottomPrice }}</td>
          <td>
            <span v-if="flight.checkStatus === 2"
              >已匹配(
              <router-link
                :to="`/bill/detail/0?billNum=` + flight.billNum"
                target="_blank"
                >{{ flight.billNum }}</router-link
              >

              )
            </span>
          </td>
        </tr>
      </tbody>
    </table>

    <div class="card-footer">
      <nav class="float-end">
        <my-pagination
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </div>
  </div>
</template>

<script>
import MyPagination from "@/components/my-pagination.vue";
import { searchTpr } from "@/api/flight";

export default {
  components: {
    MyPagination,
  },
  props: {
    etdzDate: {
      type: String,
      required: true,
    },
    ticketStatus: {
      type: Number,
      required: true,
    },
    intlTicket: {
      type: Number,
      default: -1,
    },
    pageSize: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      dataList: [],
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 25,
        pageTotal: 0,
      },
      totalInfo: {
        normalFareAmount: 0,
        carriersAmount: 0,
        normalTaxAmount: 0,
        normalCommitAmount: 0,
        normalMatchedCount: 0,
        normalTotalCost: 0,

        netRefundAmount: 0,
        deductionAmount: 0,
        refundTaxAmount: 0,
        refundCommitAmount: 0,
        refundMatchedCount: 0,
      },
    };
  },
  watch: {
    etdzDate: function () {
      this.search();
    },
    ticketStatus: function () {
      this.search();
    },
    intlTicket: function () {
      this.search();
    },
    pageSize: function () {
      this.search();
    },
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", {
        loading: false,
      });
    },
    search: function () {
      if (this.etdzDate.length === 0) {
        return;
      }

      let params = {
        beginDate: this.etdzDate,
        endDate: this.etdzDate,
        ticketStatus: this.ticketStatus,
        type3: this.intlTicket,
        pageSize: this.pageSize,
        pageNo: this.sc.pageNo,
      };

      this.showLoading();

      searchTpr(
        params,
        (jsonResult) => {
          this.dataList = jsonResult.dataList;
          this.sc = jsonResult.page;
          this.calcTotal();
        },
        () => {
          this.hideLoading();
        }
      );
    },
    calcTotal: function () {
      this.resetTotal();
      this.dataList.forEach((item, index, arr) => {
        if (item.ticketStatus === 0 || item.ticketStatus === 1) {
          //出票、改签都算正常票
          this.totalInfo.normalFareAmount += item.price;
          this.totalInfo.normalTaxAmount += item.tax;
          this.totalInfo.normalCommitAmount += item.commission;
          this.totalInfo.carriersAmount += item.price - item.commission;
          this.totalInfo.normalTotalCost +=
            item.price - item.commission + item.tax;
          if (item.checkStatus === 2) {
            this.totalInfo.normalMatchedCount += 1;
          }
        } else if (item.ticketStatus === 4) {
          //退票
          this.totalInfo.netRefundAmount += item.price;
          this.totalInfo.deductionAmount += item.tax;
          this.totalInfo.refundTaxAmount += item.tax;
          this.totalInfo.refundCommit += item.commission;
          if (item.checkStatus === 2) {
            this.totalInfo.refundMatchedCount += 1;
          }
        }
      });
    },
    resetTotal: function () {
      this.totalInfo.normalFareAmount = 0;
      this.totalInfo.normalTaxAmount = 0;
      this.totalInfo.normalCommitAmount = 0;
      this.totalInfo.carriersAmount = 0;
      this.totalInfo.normalMatchedCount = 0;
      this.totalInfo.normalTotalCost = 0;

      this.totalInfo.netRefundAmount = 0;
      this.totalInfo.deductionAmount = 0;
      this.totalInfo.refundTaxAmount = 0;
      this.totalInfo.refundCommit = 0;
      this.totalInfo.refundMatchedCount = 0;
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>